package com.app.admin;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.app.R;
import com.app.util.DBHelper;

public class Detail extends AppCompatActivity {

    private EditText userId;
    private EditText username;
    private RadioGroup sex;
    private RadioButton man;
    private RadioButton woman;
    private EditText phone;
    private EditText idcard;
    private AppCompatButton confirm;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        id = getIntent().getIntExtra("id", 0);
        initView();
        setData();

        confirm.setOnClickListener(v -> {
            String updatedUserId = userId.getText().toString();
            String updatedUserName = username.getText().toString();
            String updatedPhoneNumber = phone.getText().toString();
            String updatedIdCard = idcard.getText().toString();
            int updatedGender = man.isChecked() ? 0 : 1;

            DBHelper dbHelper = new DBHelper(this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put("user_id", updatedUserId);
            values.put("username", updatedUserName);
            values.put("gender", updatedGender);
            values.put("phone_number", updatedPhoneNumber);
            values.put("id_card", updatedIdCard);

            int rowsUpdated = db.update("user", values, "id=?", new String[]{String.valueOf(id)});
            if (rowsUpdated > 0) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
            }
            dbHelper.close();
            db.close();
        });
    }

    private void setData() {
        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sqlQuery = "SELECT * FROM user WHERE id = ?";
        Cursor cursor = db.rawQuery(sqlQuery, new String[]{String.valueOf(id)});
        if (cursor.moveToFirst()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            String userName = cursor.getString(cursor.getColumnIndex("username"));
            int gender = cursor.getInt(cursor.getColumnIndex("gender"));
            String phoneNumber = cursor.getString(cursor.getColumnIndex("phone_number"));
            String idCard = cursor.getString(cursor.getColumnIndex("id_card"));
            userId.setText(user_id);
            username.setText(userName);
            phone.setText(phoneNumber);
            idcard.setText(idCard);
            if (gender == 0) {
                man.setChecked(true);
            } else if (gender == 1) {
                woman.setChecked(true);
            }
        }
        dbHelper.close();
        cursor.close();
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("用户信息");

        userId = (EditText) findViewById(R.id.user_id);
        username = (EditText) findViewById(R.id.username);
        sex = (RadioGroup) findViewById(R.id.sex);
        man = (RadioButton) findViewById(R.id.man);
        woman = (RadioButton) findViewById(R.id.woman);
        phone = (EditText) findViewById(R.id.phone);
        idcard = (EditText) findViewById(R.id.idcard);
        confirm = (AppCompatButton) findViewById(R.id.confirm);
    }
}